//
//  HWMeFootView.m
//  JWC
//
//  Created by lanou on 16/4/7.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWMeFootView.h"
#import "HWMeTool.h"
#import "HWButton.h"
#import "HWWebviewController.h"


@implementation HWMeFootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        [self createItem];
    }
    return self;
}

#pragma mark --- 创建按钮 ---
- (void)createItem
{
    // 有4列button
    NSInteger maxRow = 4;
    CGFloat btnW = SCREEN_WIDTH / 4;
    CGFloat btnH = btnW;
    HWMeTool *tool = [[HWMeTool alloc]init];
    [tool getToolData:^(NSArray *meArray)
     {
         NSMutableArray *modelArray = [NSMutableArray array];
         for (HWMeModel *model in meArray)
         {
             if ([model.url hasPrefix:@"http"] || [model.id isEqualToString:@"0"])
             {
                 [modelArray addObject:model];
             }
         }
         for (NSInteger i = 0; i < modelArray.count; i++)
         {
             HWButton *button = [[HWButton alloc]init];
             button.model = modelArray[i];
             NSInteger list = i % maxRow;  // 第几列
             NSInteger row = i / maxRow;  // 第几行
             CGFloat btnX = list * btnW;  // 坐标X
             CGFloat btnY = row * btnH;  // 坐标Y
             button.frame = CGRectMake(btnX, btnY, btnW, btnH);
             [self addSubview:button];
             
             // 点击button
             [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(HWButton *button)
              {
                  if (![button.model.url hasPrefix:@"http"]) return;
                  HWWebviewController *web = [[HWWebviewController alloc]init];
                  web.url = button.model.url;
                  web.title = button.model.name;
                  
                  // 取出当前导航控制器
                  UITabBarController *tabBarVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                  UINavigationController *nav = (UINavigationController *)tabBarVc.selectedViewController;
                  [nav pushViewController:web animated:YES];
              }];
         }
         // 算出footer的高度
         self.height = (modelArray.count + maxRow - 1) / maxRow * btnH;
         [self setNeedsDisplay];
     }];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
